package com.fzy.leetcode.editor.cn;
//2023-03-17 10:55:09
//输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。 
//
// 
//
// 示例 1： 
//
// 输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
//输出：[1,2,3,6,9,8,7,4,5]
// 
//
// 示例 2： 
//
// 输入：matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
//输出：[1,2,3,4,8,12,11,10,9,5,6,7]
// 
//
// 
//
// 限制： 
//
// 
// 0 <= matrix.length <= 100 
// 0 <= matrix[i].length <= 100 
// 
//
// 注意：本题与主站 54 题相同：https://leetcode-cn.com/problems/spiral-matrix/ 
//
// Related Topics 数组 矩阵 模拟 👍 509 👎 0

class ShunShiZhenDaYinJuZhenLcof {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new ShunShiZhenDaYinJuZhenLcof().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return new int[0];
        }
        int m = matrix.length;
        int n = matrix[0].length;

        int[] ans = new int[m*n];

        int start = 0,end = n-1;
        int low = 0,high = m-1;
        for(int i = 0;i<m*n;){
            for(int j = start;j<=end;j++){
                if(i == m*n){
                    return ans;
                }
                ans[i] = matrix[low][j];
                i++;
            }
            low++;
            for(int j = low;j<=high;j++){
                if(i == m*n){
                    return ans;
                }
                ans[i] = matrix[j][end];
                i++;
            }
            end--;
            for (int j = end;j>=start;j--){
                if(i == m*n){
                    return ans;
                }
                ans[i] = matrix[high][j];
                i++;
            }
            high--;
            for(int j = high;j>=low;j--){
                if(i == m*n){
                    return ans;
                }
                ans[i] = matrix[j][start];
                i++;
            }
            start++;
        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}